package Arrays;

/**
 * 作者:hfj
 * 功能:190.颠倒二进制位
 * 日期: 2025/10/23 11:18
 */
public class ReverseBits {
    public static void main(String[] args) {
        int n = 43261596;
        System.out.println(reverseBits(n));
    }


    public static int reverseBits(int n) {

        //求出二进制放入builder
        StringBuilder builder = new StringBuilder();
        int l;
        while (n > 0){
            l = n % 2;
            builder.append(l);
            n /= 2;
        }

        //补全整形的二进制位
        int sum = 0,k=1;
        while (builder.length() < 32){
            builder.append("0");
        }

        //计算反转过来的二进制位
        for (int i = builder.length() -1; i >= 0; i--) {
            if (builder.charAt(i) == '1'){
                sum += k;
            }
            k *= 2;
        }
        return sum;//返回
    }
}
